FileData = function(element){
	var f = new MVC.JFile(element.id);
	this.name = f.file_and_extension();
	this.type =  element.getAttribute('type');
	this.path = element.id;
};
/*FileData.prototype = {
	name: function(){
		return new MVC.JFile(this.element.id).file_and_extension();
	},
	type : function(){
		return this.element.getAttribute('type');
	},
	path : function(){
		return element.id;
	}
}*/
File = {};

File.sort = function(fileA, fileB) {
	if(fileA.type > fileB.type) return 1;  //file > directory
	if(fileA.type < fileB.type) return -1; //file < directory
	var alc = fileA.name.toLowerCase(), blc = fileB.name.toLowerCase();
	if(alc > blc)	return 1;
	return alc.toLowerCase() < blc.toLowerCase() ? -1 : 0;
};
File.close = function(){
};
File.save = function(edit_area_id, text){
	var file_id = editAreaLoader.getCurrentFile(edit_area_id).id; 
    var el = MVC.$E(file_id);
	if(!el && file_id) return;

	var saved_function = function(response){
		eval('var object = '+response.responseText);
	    object.response == 'Save' ? editAreaLoader.setFileEditedMode(edit_area_id, file_id, false) : alert('save unsucessful');
		el.innerHTML = new MVC.JFile(el.id).file_and_extension();
	};
	new MVC.Ajax('/ftp/save?path='+file_id, {method: 'post', postBody: 'text='+encodeURIComponent(text), onComplete: saved_function } );
	el.innerHTML = 'Saving ...';
};

File.file_name = function(element){
	return element.id.match(/[^\/]*$/)[0].toLowerCase();
};
FileView = {};
FileView.insert = function(path, files_container)
{
	var name = path.match(/[^\/]*$/)[0], ret;
	var div = document.createElement('div');
	div.id = path;
	div.className = 'file';
	div.setAttribute('type', 'file');
	div.innerHTML = name;
	
	if(!files_container){
		files_container = new MVC.JFile(path).container();
	}

	
	var files = files_container.childNodes; //this is ok as long as we aren't doing html
	if(files.length == 0){
		files_container.appendChild(div);
		//new Draggable(div, {revert:true} );
		return div;
	}

	//check if we can add to the last right away
	var last = files[files.length - 1];
	if(name.toLowerCase() >  File.file_name(last) || last.getAttribute('type') == 'directory'){
		files_container.appendChild(div);
		//new Draggable(div, {revert:true} );
		return div;
	}

	for(var i = 0 ; i < files.length; i++)
	{
		var filename = File.file_name(files[i]);
		var type = files[i].getAttribute('type');

		if( name.toLowerCase() <  filename.toLowerCase() && type != 'directory') {
			files_container.insertBefore(div,files[i] );
			//new Draggable(div, {revert:true} );
			return div;
		}
	}
};


